<?php

class progress extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('progress_model');
    }

    public function diagnosticUpdate($pass = '') {
        $data = $this->progress_model->getStudentProfile($this->session->userdata('user_id'));
        $id = $data['user_id'];
        if ($data['level_number'] == 0) {
            if ($pass == 'pass') {
                $data['level_number']+=1;
                $data = array('level_number' => $data['level_number']);
                $this->updateLevel($id, $data);
                $data['level_number']+=1;
                $item['location'] = base_url('story/diagnostic/' . $data['level_number'] . '');
                $item['message'] = 'Congratulations! You have passed the diagnostic exam for the Red level. You will advance to the next color.';
                $item['title'] = 'Message';
                $this->load->view('templates/header', $item);
                $this->load->view('message', $item);
                $this->load->view('templates/footer');
            } else {
                $data = array('level_number' => '1');
                $this->updateLevel($id, $data);
                $item['location'] = base_url('student/display/');
                $item['message'] = 'Congratulations! You have finished the diagnostic exam. You will start at the Red level.';
                $item['title'] = 'Message';
                $this->load->view('templates/header', $item);
                $this->load->view('message', $item);
                $this->load->view('templates/footer');
            }
        } else {
            if ($pass == 'pass') {
                $data['level_number'] +=1;
                if ($data['level_number'] > 5) {
                    $data = array('level_number' => $data['level_number']);
                    $this->updateLevel($id, $data);
                    $item['location'] = base_url('student/display/');
                    $item['message'] = 'Congratulations! You have passed all the diagnostic exams. You will start at the Lime level.';
                    $item['title'] = 'Message';
                    $this->load->view('templates/header', $item);
                    $this->load->view('message', $item);
                    $this->load->view('templates/footer');
                } else {
                    $data = array('level_number' => $data['level_number']);
                    $this->updateLevel($id, $data);
                    $data['level_number']+=1;
                    $item['location'] = base_url('story/diagnostic/' . $data['level_number'] . '');
                    $color = $this->progress_model->getColorLevel(($data['level_number'] - 1));
                    $item['message'] = 'Congratulations! You have passed the diagnostic exam for the '.$color['level_name'].' level. You will advance to the next level.';
                    $item['title'] = 'Message';
                    $this->load->view('templates/header', $item);
                    $this->load->view('message', $item);
                    $this->load->view('templates/footer');
                }
            } else {
                $color = $this->progress_model->getColorLevel(($data['level_number']));
                $item['location'] = base_url('student/display/');
                $item['message'] = 'Congratulations! You have finished the diagnostic exam. You will start at the  '.$color['level_name'].' level.';
                $item['title'] = 'Message';
                $this->load->view('templates/header', $item);
                $this->load->view('message', $item);
                $this->load->view('templates/footer');
            }
        }
    }

    public function index($score) {
        $this->updateStatus($score);
        $userid = $this->session->userdata('user_id');
        $storyid = $_SESSION['story_id'];
        if ($score > 79) {
            $passed = 'Y';
        } else {
            $passed = 'N';
        }
        $time = $_SESSION['time'];
        $data = array('user_id' => $userid, 'hasPassed' => $passed, 'duration' => $time, 'story_id' => $storyid, 'score' => $score);
        $this->progress_model->insertProgress($data);
        redirect(base_url('/progress/message/'.$passed));
        
    }
    public function message($passed){
        $item['location'] = base_url('student/dashboard');
        if ($passed == 'N') {
            $item['message'] = 'Sorry! You did not passed the previous exam. You will be redirected to your home page.';
        } else if ($passed == 'Y') {
            $item['message'] = 'Congratulations! You have passed the exam.';
        }
        $item['title'] = 'Message';
        $this->load->view('templates/header', $item);
        $this->load->view('message', $item);
        $this->load->view('templates/footer');
    }

    public function updateLevel($id, $data) {
        $this->progress_model->updateStudent($id, $data);
        $this->resetStatus();
    }

    public function getStoryCriteria($storyid) {
        $story = $this->progress_model->getStoryDetails($storyid);
        $criteria = $story['criteria_id'];
        return $criteria;
    }

    public function updateStatus($score) {
        $id =$this->session->userdata('user_id');
        $storyid = $_SESSION['story_id'];
        $criteria = $this->getStoryCriteria($storyid);
        $param = array('user_id' => $id, 'criteria_id' => $criteria);
        if ($score > 79) {
            $data = array('state' => 'A');
            $this->progress_model->updateStatus($param, $data);
        } else {
            $data = array('state' => 'C');
            $this->progress_model->updateStatus($param, $data);
        }
        return;
    }

    public function onLoadUpdate() {
        $id = $this->session->userdata('user_id');
        $level = $this->getlevel($id);
        $level +=1;
        $data = array('level_number' => $level);
        $this->updateLevel($id, $data);
        $item['location'] = base_url('student/dashboard/');
        $item['message'] = 'Congratulations! You are now eligible to transfer to next color level.';
        $item['title'] = 'Message';
        $this->load->view('templates/header', $item);
        $this->load->view('message', $item);
        $this->load->view('templates/footer');
    }

    public function getlevel($userid) {
        $level = $this->progress_model->getStudentProfile($userid);
        $level = $level['level_number'];
        return $level;
    }

    public function resetStatus() {
        $userid = $this->session->userdata('user_id');
        $level = $this->getlevel($userid);

        $this->progress_model->refreshStatus($userid, array('doesExist' => 'N', 'state' => 'B'), '');
        $levelgroup1 = array(11, 6, 7, 8);
        $levelgroup2 = array(9, 10, 12, 5);

        if ($level == '1') {
            $criteria = array(1, 2, 3, 4, 5, 6, 7);
            $this->progress_model->refreshStatus($userid, array('doesExist' => 'Y', 'state' => 'B'), $criteria);
        } else if ($level == '3') {
            $criteria = array(1, 2, 3, 4, 6, 7, 9);
            $this->progress_model->refreshStatus($userid, array('doesExist' => 'Y', 'state' => 'B'), $criteria);
        } else if ($level == '4') {
            $criteria = array(1, 2, 3, 4, 5, 6, 9);
            $this->progress_model->refreshStatus($userid, array('doesExist' => 'Y', 'state' => 'B'), $criteria);
        } else if ($level == '2') {
            $criteria = array(1, 2, 3, 5, 6, 7, 8);
            $this->progress_model->refreshStatus($userid, array('doesExist' => 'Y', 'state' => 'B'), $criteria);
        } else if (in_array($level, $levelgroup1)) {
            $criteria = array(1, 2, 3, 4, 5, 6);
            $this->progress_model->refreshStatus($userid, array('doesExist' => 'Y', 'state' => 'B'), $criteria);
        } else if (in_array($level, $levelgroup2)) {
            $criteria = array(1, 2, 3, 4, 5, 6, 8);
            $this->progress_model->refreshStatus($userid, array('doesExist' => 'Y', 'state' => 'B'), $criteria);
        }
        return;
    }

}

?>
